home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / visulztn / saoimage / saoimage.lha / ctrlgc.c < prev    next >
C/C++ Source or Header  |  1990-05-02  |  9KB  |  294 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    ctrlgc.c (Control Graphics Context)
  6.  * Purpose:    Create a GC and set the GC params when needed
  7.  * Subroutine:    init_gc()            returns: void
  8.  * Subroutine:    free_gc()            returns: void
  9.  * Subroutine:    set_gc()            returns: GC
  10.  * Subroutine:    set_gc_with_background()    returns: GC
  11.  * Subroutine:    set_text_gc()            returns: GC
  12.  * Subroutine:    set_edit_gc()            returns: GC
  13.  * Subroutine:    get_fontstruct()        returns: XFontStruct *
  14.  * Xlib calls:    XCreateGC(), XSetMask(), XSetFunction(), XSetForeground()
  15.  * Xlib calls:    XChangeGC(), XLoadQueryFont()
  16.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  17.  *        You may do anything you like with this file except remove
  18.  *        this copyright.  The Smithsonian Astrophysical Observatory
  19.  *        makes no representations about the suitability of this
  20.  *        software for any purpose.  It is provided "as is" without
  21.  *        express or implied warranty.
  22.  * Modified:    {0} Michael VanHilst    initial version          29 May 1989
  23.  *        {n} <who> -- <does what> -- <when>
  24.  */
  25.  
  26. #include <stdio.h>        /* get stderr, NULL, etc */
  27. #include <X11/Xlib.h>        /* X window stuff */
  28. #include <X11/Xutil.h>        /* X window manager stuff */
  29. #include "hfiles/define.h"    /* define DONT_CARE, U_DONT_CARE */
  30. #include "hfiles/color.h"    /* color and GCspec param structure */
  31. extern struct colorRec color;
  32.  
  33. static GC main_gc = NULL;
  34. static Display *main_display;
  35. static XGCValues gcvalue;
  36.  
  37. #define OPTION_COUNT 6
  38. #define NAME_COUNT 17
  39. static char *fontlist[NAME_COUNT] = {
  40.   NULL,                /* user preference 0 */
  41.   NULL,                /* user preference 1 */
  42.   NULL,                /* user preference 2 */
  43.   NULL,                        /* 3 */
  44.   "6x10",                    /* 4 */
  45.   "6x13",                    /* 5 */
  46.   "8x13",                    /* 6 */
  47.   "9x15",                    /* 7 */
  48.   "*-courier-medium-r-normal-*-10-*",        /* 8 */
  49.   "*-helvetica-medium-r-normal-*-10-*",        /* 9 */
  50.   "*-times-medium-r-normal-*-10-*",        /* 10 */
  51.   "*-courier-medium-r-normal-*-12-*",        /* 11 */
  52.   "*-helvetica-medium-r-normal-*-12-*",        /* 12 */
  53.   "*-times-medium-r-normal-*-12-*",        /* 13 */
  54.   "*-courier-medium-r-normal-*-14-*",        /* 14 */
  55.   "*-helvetica-medium-r-normal-*-14-*",        /* 15 */
  56.   "*-times-medium-r-normal-*-14-*" };        /* 16 */
  57. static XFontStruct *fontstruct[NAME_COUNT] =
  58.   { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  59.       NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  60. /* order to try fonts from list for particular application type */ 
  61. static int preference[3][OPTION_COUNT] = {
  62.   { 0,  5,  9,  10,  4, 8 },    /* small for color graph */
  63.   { 1,  5, 11, 12, 13,  6 },    /* medium for cursor label */
  64.   { 2,  5,  7, 15, 16, 14 } };     /* large for popup editor */
  65. static XFontStruct *app_font[3] = { NULL, NULL, NULL };
  66.  
  67. /*
  68.  * Subroutine:    init_gc()
  69.  * Purpose:    Create the gc and set initial values
  70.  * Xlib calls:    XCreateGC()
  71.  */
  72. void init_gc ( display, window )
  73.      Display *display;
  74.      Window window;
  75. {
  76.   unsigned long valuemask;
  77.  
  78.   main_display = display;
  79.   gcvalue.function = GXcopy;
  80.   gcvalue.plane_mask = AllPlanes;
  81.   gcvalue.foreground = color.gcset.menu.foreground;
  82.   gcvalue.background = color.gcset.menu.background;
  83.   /* set mask for values which are not same as default */
  84.   valuemask = GCForeground | GCBackground;
  85.   main_gc = XCreateGC(display, window, valuemask, &gcvalue);
  86. }
  87.  
  88. /*
  89.  * Subroutine:    free_gc
  90.  * Purpose:    free resources loaded by server before exiting
  91.  */
  92. void free_gc ( )
  93. {
  94.   int i;
  95.  
  96.   for( i=0; i<NAME_COUNT; i++ ) {
  97.     if( fontstruct[i] != NULL )
  98.       XFreeFont (main_display, fontstruct[i]);
  99.   }
  100.   if( main_gc != NULL )
  101.     XFreeGC(main_display, main_gc);
  102. }
  103.   
  104. /*
  105.  * Subroutine:    set_gc()
  106.  * Purpose:    Adjust gc params as specified for simple drawing
  107.  * Xlib calls:    XSetMask(), XSetFunction(), XSetForeground()
  108.  */
  109. GC set_gc ( spec )
  110.      GCspec *spec;
  111. {
  112.   if( spec != NULL ) {
  113.     if( spec->mask != gcvalue.plane_mask ) {
  114.       gcvalue.plane_mask = spec->mask;
  115.       XSetPlaneMask(main_display, main_gc, gcvalue.plane_mask);
  116.     }
  117.     if( spec->func != gcvalue.function ) {
  118.       gcvalue.function = spec->func;
  119.       XSetFunction(main_display, main_gc, gcvalue.function);
  120.     }
  121.     if( (spec->foreground != U_DONT_CARE) &&
  122.        (spec->foreground != gcvalue.foreground) ) {
  123.       gcvalue.foreground = spec->foreground;
  124.       XSetForeground(main_display, main_gc, gcvalue.foreground);
  125.     }
  126.   }
  127.   return( main_gc );
  128. }
  129.  
  130. /*
  131.  * Subroutine:    set_gc_with_background
  132.  * Purpose:    Adjust gc params as specified for drawing with a background
  133.  * Xlib calls:    XSetMask(), XSetFunction(), XSetForeground(), XSetBackground()
  134.  */
  135. GC set_gc_with_background ( spec, background )
  136.      GCspec *spec;
  137.      unsigned long background;
  138. {
  139.   if( spec->mask != gcvalue.plane_mask ) {
  140.     gcvalue.plane_mask = spec->mask;
  141.     XSetPlaneMask(main_display, main_gc, gcvalue.plane_mask);
  142.   }
  143.   if( spec->func != gcvalue.function ) {
  144.     gcvalue.function = spec->func;
  145.     XSetFunction(main_display, main_gc, gcvalue.function);
  146.   }
  147.   if( (spec->foreground != U_DONT_CARE) &&
  148.       (spec->foreground != gcvalue.foreground) ) {
  149.     gcvalue.foreground = spec->foreground;
  150.     XSetForeground(main_display, main_gc, gcvalue.foreground);
  151.   }
  152.   if( (background != U_DONT_CARE) && (background != gcvalue.background) ) {
  153.     gcvalue.background = background;
  154.     XSetBackground(main_display, main_gc, gcvalue.background);
  155.   }
  156.   return( main_gc );
  157. }
  158.  
  159. /*
  160.  * Subroutine:    set_text_gc
  161.  * Purpose:    Adjust gc params as specified for basic text
  162.  * Xlib calls:    XChangeGC()
  163.  * Note:    Most gc values are cached, but new font flushes vals to server
  164.  */
  165. GC set_text_gc ( font, foreground, background, func, mask )
  166.      Font font;
  167.      unsigned long foreground;
  168.      unsigned long background;
  169.      int func;
  170.      unsigned long mask;
  171. {
  172.   unsigned long valuemask = 0;
  173.  
  174.   if( font != gcvalue.font ) {
  175.     gcvalue.font = font;
  176.     valuemask = GCFont;
  177.   }
  178.   if( foreground != gcvalue.foreground ) {
  179.     gcvalue.foreground = foreground;
  180.     valuemask |= GCForeground;
  181.   }
  182.   if( (background != U_DONT_CARE) && (background != gcvalue.background) ) {
  183.     gcvalue.background = background;
  184.     XSetBackground(main_display, main_gc, gcvalue.background);
  185.   }
  186.   if( mask != gcvalue.plane_mask ) {
  187.     gcvalue.plane_mask = mask;
  188.     valuemask |= GCPlaneMask;
  189.   }
  190.   if( func != gcvalue.function ) {
  191.     gcvalue.function = func;
  192.     valuemask |= GCFunction;
  193.   }
  194.   if( valuemask )
  195.     XChangeGC(main_display, main_gc, valuemask, &gcvalue);
  196.   return( main_gc );
  197. }
  198.  
  199. /*
  200.  * Subroutine:    set_edit_gc
  201.  * Purpose:    Adjust gc params as specified for text with a background
  202.  * Xlib calls:    XChangeGC()
  203.  */
  204. GC set_edit_gc ( font, foreground, background )
  205.      Font font;
  206.      unsigned long foreground;
  207.      unsigned long background;
  208. {
  209.   unsigned long valuemask = 0;
  210.  
  211.   if( font != gcvalue.font ) {
  212.     gcvalue.font = font;
  213.     valuemask = GCFont;
  214.   }
  215.   if( AllPlanes != gcvalue.plane_mask ) {
  216.     gcvalue.plane_mask = AllPlanes;
  217.     valuemask |= GCPlaneMask;
  218.   }
  219.   if( GXcopy != gcvalue.function ) {
  220.     gcvalue.function = GXcopy;
  221.     valuemask |= GCFunction;
  222.   }
  223.   if( foreground != gcvalue.foreground ) {
  224.     gcvalue.foreground = foreground;
  225.     valuemask |= GCForeground;
  226.   }
  227.   if( background != gcvalue.background ) {
  228.     gcvalue.background = background;
  229.     valuemask |= GCBackground;
  230.   }
  231.   if( valuemask )
  232.     XChangeGC(main_display, main_gc, valuemask, &gcvalue);
  233.   return( main_gc );
  234. }
  235.  
  236. /*
  237.  * Subroutine:    get_fontstruct
  238.  * Returns:    Pointer to the specified fontstruct
  239.  */
  240. XFontStruct *get_fontstruct ( app_code )
  241.      int app_code;    /* i: see comments above */
  242. {
  243.   static int init_font();
  244.  
  245.   if( (app_font[app_code] == NULL) && (init_font(app_code) == NULL) )
  246.     return( NULL );
  247.   else
  248.     return( app_font[app_code] );
  249. }
  250.  
  251. /*
  252.  * Subroutine:    init_font
  253.  * Purpose:    Load the fonts used by this program
  254.  * Returns:    Font on success, else 0
  255.  */
  256. static int init_font ( app_code )
  257.      int app_code;    /* i: font application type index */
  258. {
  259.   int i, name_index;
  260.   static int open_font();
  261.  
  262.   for( i=0; i<OPTION_COUNT; i++ ) {
  263.     name_index = preference[app_code][i];
  264.     if( (fontstruct[name_index] != NULL) || open_font(name_index) ) {
  265.       app_font[app_code] = fontstruct[name_index];
  266.       return( 1 );
  267.     }
  268.   }
  269.   return( 0 );
  270. }
  271.  
  272. /*
  273.  * Subroutine:    open_font
  274.  * Purpose:    Load a font or announce failure
  275.  * Returns:    1 on success, else 0
  276.  * Xlib calls:    XLoadQueryFont()
  277.  */
  278. static int open_font ( index )
  279.      int index;        /* i: index of font name in font_name array */
  280. {
  281.   char *name;
  282.  
  283.   if( (name = fontlist[index]) != NULL ) {
  284.     if( (fontstruct[index] = XLoadQueryFont(main_display, name)) != NULL ) {
  285.       return( 1 );
  286.     } else {
  287.       /* report failure and scratch name off of list */
  288.       (void)fprintf(stderr, "Could not open font: %s\n", name);
  289.       fontlist[index] = NULL;
  290.     }
  291.   }
  292.   return( 0 );
  293. }
  294.